﻿#include "widget.h"
#include "ui_widget.h"

#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QFileDialog>
#include <QFile>
#include <QTextStream>
#include <QColorDialog>
#include <QFontDialog>
#include <QDebug>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    connect(ui->btn_open, &QPushButton::clicked, this, &Widget::openFile);
    connect(ui->btn_save, &QPushButton::clicked, this, &Widget::saveFile);
    connect(ui->btn_color, &QPushButton::clicked, this, &Widget::setColor);
    connect(ui->btn_font, &QPushButton::clicked, this, &Widget::set_Font);

}

Widget::~Widget()
{
    delete ui;
}

class Hero
{
public:
    QString name;
    int def;
    int atk;
};

class Person
{
public:
    QString name;
    int def;
    int atk;
};


void Widget::openFile()
{
    QFile data("1.txt");
        if(data.open(QIODevice::WriteOnly|QIODevice::Truncate))
        {
            QDataStream out(&data);
            Hero hero={"德玛西亚", 100,97};
            Person person={"sadad",50,50};

            out<<hero.name<<hero.def<<hero.atk;
            out<<person.name<<person.def<<person.atk;
        }
        data.close();

        data.setFileName("1.txt");
        if(data.open(QIODevice::ReadOnly))
        {
            Hero hero;
            Person person;
            QDataStream in(&data);

            in>>person.name>>person.def>>person.atk;
            qDebug()<<person.name<<person.def<<person.atk<<endl;

            in>>hero.name>>hero.def>>hero.atk;
            qDebug()<<hero.name<<hero.def<<hero.atk<<endl;
        }
        data.close();


#if 0
    QString filename = QFileDialog::getOpenFileName(this, tr("Open File"),
                                                    "./test_source",
                                                    tr("Text files (*.txt);;Images (*.png *.xpm *.jpg *.gif);;XML files (*.xml);; C/C++ sourcs(*.c *.cpp)"));

    ui->lineEdit->setText(filename);

    /* 构建文件类对象 */
    QFile file(filename);

    /* 以只读模式打开文本文件 */
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return;


    QByteArray data = file.readAll();
    qDebug() << "ok";
    ui->textEdit->setText(data);
    /* 将文件转换为文本流 */
    QTextStream in(&file);
    /* 文本流中读取所有数据写入到QtextEdit控件中 */
    ui->textEdit->setText(in.readAll());
#endif
}

void Widget::saveFile()
{
    QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),
                                                    "./test_source",
                                                    tr("Text files (*.txt);;Images (*.png *.xpm *.jpg *.gif);;XML files (*.xml);; C/C++ sourcs(*.c *.cpp)"));
    ui->lineEdit->setText(fileName);
    /* 构建文件类对象 */
    QFile file(fileName);

    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
        return;

    QTextStream out(&file);             /* 将文件转换为文本流 */
    out << ui->textEdit->toPlainText();     /* 获取QTextEdit中的数据并重定向写入到文件中 */
    file.close();
}

void Widget::setColor()
{
    QColor color = QColorDialog::getColor();
    ui->textEdit->setTextColor(color);
}

void Widget::set_Font()
{
    bool ok;
    QFont font = QFontDialog::getFont(&ok, QFont("Times",12), this);
    if(ok)
    {
        ui->textEdit->setFont(font);
    }
}
